package client;

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.io.Serializable;
import java.net.*;

import adapter.IAdapter;
import adapter.IClientCallback;

public class RMIClient extends UnicastRemoteObject implements IClientCallback 
{
	private static final long serialVersionUID = 0L;
	
	public IAdapter m_Adapter = null;
	private int m_RMIPort;
	private String m_RMIHost;
	private String m_RMIServerURL = null;
	
	public RMIClient(String n_RMIHost, int n_RMIPort) throws RemoteException
	{
		m_RMIPort = n_RMIPort;
		m_RMIHost = n_RMIHost;
		m_RMIServerURL = String.format("rmi://%s:%d/FlughafenAdapter", m_RMIHost, m_RMIPort);
		
		try 
		{
			// Connect to RMI Adapter
			m_Adapter = (IAdapter) Naming.lookup(m_RMIServerURL);
			System.out.println("Adapter connected to RMI Registry: " + m_RMIServerURL);
			
			//Client bei RMI Adapter registrieren
			m_Adapter.registerClientCallback(this);
		}
		catch (NotBoundException e) {
			e.printStackTrace();
		}
		
		catch (MalformedURLException e) 
		{
			System.out.println("URL ungültig:\n" + e.getMessage());
		}
	}

	@Override
	public void update(IAdapter adapter) throws RemoteException {
		System.out.println("Update function");
	}
}